package com.item.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.base.util.DateUtil;
import com.item.dao.model.NoteAuthor;
import com.item.dao.model.NoteAuthorExample;
import com.item.dao.model.NoteAuthorExample.Criteria;
import com.item.exception.JSONRepUtils;
import com.item.service.NoteAuthorService;
import com.item.service.NoteAuthorService;
import com.item.util.SubjectUtils;

@Controller
public class NoteAuthorController {
	@Autowired
	private NoteAuthorService authorService;
	
	@RequestMapping("/listNoteAuthor")
	@ResponseBody
	public String list(Page param,NoteAuthor author) throws Exception{
		NoteAuthorExample example = new NoteAuthorExample();
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);
		Criteria criteria = example.createCriteria();
		
		//以下两个字段用来做筛选  分别是title和source的模糊匹配
		if(StringUtils.isNotBlank(author.getName())){
			criteria.andNameLike("%"+author.getName()+"%");
		}
		if(StringUtils.isNotBlank(author.getDescription())){
			criteria.andDescriptionLike("%" + author.getDescription() +"%");
		}
		if(author.getSex() != null){
			criteria.andSexEqualTo(author.getSex());
		}
		example.setOrderByClause("name DESC");
		List<NoteAuthor> list = authorService.selectByExample(example);
		return JSONRepUtils.serialize(new Page(PaginationSupport.getContext().getTotalCount(), list), 0);
	}
	
	@RequestMapping("/showlistNoteAuthor")
	public String list() {
		return "author/authorList";
	}
	
	@RequestMapping("/toEditNoteAuthor")
	public String toEdit()throws Exception{
		return "/author/authorEdit";
	}
	
	@RequestMapping(value="/saveNoteAuthor",method=RequestMethod.POST)
	@ResponseBody
	public String save(NoteAuthor author, HttpSession session)throws Exception{
		try{
			if(StringUtils.isBlank(author.getId())){
				author.setImgPath("");
				author.setCreateTime(new Date());
				author.setOtherImgs(author.getOtherImgs().replaceAll("\\,", ";"));
				authorService.insert(author);
			}else{
				authorService.updateByPrimaryKeySelective(author);
			}
		}catch(Exception e){
			e.printStackTrace();
			return "{\"success\":false}";
		}
		return "{\"success\":true}";
	}
	
	@RequestMapping(value="/findByIdNoteAuthor")
	@ResponseBody
	public String findById(String id,HttpSession session)throws Exception{		
		NoteAuthor n = authorService.selectByPrimaryKey(id);
		n.setOtherImgs(n.getOtherImgs().replaceAll(";", " ").trim().replaceAll(" ", "\\,"));
		return JSONRepUtils.serialize(n, 0);
	}
	
	@RequestMapping("/delNoteAuthor")
	@ResponseBody
	public String del(String id,String url,HttpSession session)throws Exception{
//		WebPageFileController fileController = new WebPageFileController();
		String[] ids = id.split(",");
		int len = ids.length;
		for(int i = 0; i < len; i++) {
			authorService.deleteByPrimaryKey(ids[i]);
//			fileController.deleteFile(urls[i]);
		}
		return "{\"success\":true}";
	}
	
}

